<?php
	$ils = Rd_Registry::get('root:ils');	
	$onePerPage = array_key_exists('onePerPage', $this->request) && 'yes' == $this->request['onePerPage'];
?>
<script type="text/javascript">
	function confirmation(type, address) {
		if (type == "delete"){
			message = "Are you sure you want to Delete this reserve?";
		}
		if (type == "denyCopyright"){
			message = "Are you sure you want to Deny Copyright to this class for this reserve?";
		}
		if (type == "denyCopyrightAll"){
			message = "Are you sure you want to Deny Copyright FOR ALL USERS for this item?";
		}
		var answer = confirm(message);
		if(answer){
			window.location = address;
			return true;
		}
		else{
			return false;
		}
	}

	jQuery(document).ready(function() { 
		var details = jQuery('div.queueMetadata table.details');
		var detailsImgs = jQuery("div.queueMetadata img.detailsImg");
		jQuery('div.detailsButton').click(function(){
			jQuery(this).parent().find("table.details").toggle();
			if (jQuery(this).parent().find("table.details").is(":visible")) {
				jQuery(this).find("img.detailsImg").attr("src","public/images/minimize.gif");
			}
			else {
				jQuery(this).find("img.detailsImg").attr("src","public/images/maximize.gif");
			}
			return false;
		});
		
		jQuery("span.ExpandAll").click(function() {	
			detailsImgs.attr("src", "public/images/minimize.gif");
			details.show();
			return false;
		});
		jQuery("span.CollapseAll").click(function(){
			detailsImgs.attr("src", "public/images/maximize.gif");
			details.hide();
			return false;
		});
	});
</script>
<table class="queue">
	<tr class="category">
<?php if (!$this->printView) { ?>
		<th>
			<input type="checkbox" onchange="javascript: checkAll(this.form, this.checked);" />
		</th>
<?php } ?>
<?php
		$sortOptions = array(
			'date', 
			'type', 
			'requested', 
			'needed', 
			'semester', 
			'class', 
			'instructor', 
			'callnum'
		);
		$sortTitles = array(
			'ID', 
			'Type', 
			'Requested', 
			'Needed By', 
			'Semester', 
			'Course', 
			'Instructor(s)', 
			'Call #'
		);
		
		foreach($sortOptions as $sortIndex => $sortOption){
			$sortClass = 
				$sortOption == $this->sort 
				? (" class=\"sort-{$this->dir}\"")
				: '';
			$sortLabel = $sortTitles[$sortIndex];
			$newDir = (
				$sortOption == $this->sort
				? (
					$this->dir == 'asc' ? 'desc' : 'asc'
				) : $this->dir
			);
			$sortUrl = "index.php?cmd={$this->request['cmd']}&libraryId={$this->libraryId}"
				. "&statusId={$this->statusId}&sort={$sortOption}&dir={$newDir}";
			$generatedTag = (
				!$this->printView
				? "<a href=\"{$sortUrl}\"{$sortClass}>{$sortLabel}</a>"
				: "<span{$sortClass}>{$sortLabel}</span>"
			);
?>
		<th <?php print($sortIndex == 0 ? 'id="rid"' : ''); ?>>
			<?php print($generatedTag); ?>
		</th>
<?php 
		}
?>
	</tr>
<?php
		$cnt = 0;
		foreach ($this->requestList as $r)
		{
			$item = $r->requestedItem;
			$ci = $r->courseInstance;
			
			$pCopy = $item->physicalCopy;
			
			
			if($pCopy->getCallNumber()){
				$callnum = $pCopy->getCallNumber();	
			} elseif($item->getOldId()) {
				$callnum = $item->getOldId() . ' <b>(Tentative)</b>';	
			} else{
				$callnum = '';	
			}
			$requestNotes = $r->getAllNotes();
			$selectedUnit = 
				!array_key_exists('unit', $this->request) || '' ==  $this->request['unit']
				? $this->user->getStaffLibrary()
				: $this->request['unit'];
			$selectedFilter = 
				!array_key_exists('filter_status', $this->request) || '' ==  $this->request['filter_status']
				? Rd_Request_Status::getDefaultId()
				:  $this->request['filter_status'];
			
			$processCmd = ($r->isScanRequest()) ? "addDigitalItem" : "addPhysicalItem";

			$cnt++;
			
			$location = "";
			if(count($r->holdings) > 0){
				foreach ($r->holdings as $h){
					if (is_array($h)){
						$location .= "" . $h['library'] . " " . $h['callNum'] . " " . $h['loc'] . " " . $h['type'] . "<br>";
					}
				}
			}
?>
	<tr class="queueData">
<?php if (!$this->printView) { ?>
		<th id="r<?php print($r->requestID); ?>"><input type="checkbox" name="selectedRequest[]" value="<?php print($r->requestID); ?>"></th>
<?php } ?>
		<td class="dataLeft"><strong><?php print(sprintf("%06s",$r->requestID)); ?></strong></td>
		<td><?php print($r->getType()); ?></td>
		<td><?php print(Filter_DbDate::filter($r->getDateRequested())); ?></td>
		<td><?php if (Filter_DbDate::notEmpty($r->getDesiredDate())) { print(Filter_DbDate::filter($r->getDesiredDate(), TRUE, TRUE)); } ?></td>
		<td><?php print($ci->displayTerm()); ?></td>
		<td><a target="_blank" href="./?cmd=editClass&ci=<?php print($ci->getId()); ?>"><?php print($ci->course->displayCourseNo()); ?></a></td>
		<td colspan="2"><?php print($ci->displayInstructors(true)); ?></td>
	</tr>
				
	<tr <?php print( $onePerPage ? 'class="temp"' : ''); ?>>
<?php if (!$this->printView) { ?>
		<td>&nbsp;</td>
<?php } ?>
		<td class="queueItem" colspan="8" headers="rid r<?php print($r->requestID); ?>">
<?php if(!$this->printView) { ?>
			<div class="queueActions">
				<div>
					<div class="queueActionsSelect">
									<?php 
									$this->getDisplayer()->renderReserveStatusSelect(array(
										'defaultStatus' => $r,
										'showDefault' => TRUE,
										'fieldName' => $r->requestID . '_status',
										'excludeActive' => TRUE,
										'excludeInactive' => TRUE,
										'excludeDenied' => TRUE,
										'onChange' => "updateRequestStatus(this, {$r->requestID} , '#noticeDropDown_{$r->requestID}');"
									));
									?>
									<div id='noticeDropDown_<?php print($r->requestID); ?>' style='display: inline;'>
										<img class="vertical-align:middle;" width="16px" height='16px' src='public/images/spacer.gif' />
									</div>
									<?php if ('DENIED' == $item->getStatus()) { ?>
									<span class="error">Previously Denied</span>
									<?php } else if ('' == trim($item->getStatus())) {	?>
									<span class="error">Item Status Missing</span>
									<?php } else if (Account_Rd::isAdmin()) {	?>
									<span class="notice"><?php print_r($item->getStatus()); ?></span>
									<?php } ?>
								</div>
								<div class="processButtons">
									<span>
										<input type="button" class="processButton" value="Process Request" onclick="window.location='index.php?cmd=<?php print($processCmd); ?>&request_id=<?php print($r->requestID); ?>&loan_period=<?php print($r->reserve->getRequestedLoanPeriod()); ?>'" />
									</span>
									<span>
										<input type="button" class="deleteButton" value="Delete Request" onClick="confirmation('delete', 'index.php?cmd=deleteRequest&request_id=<?php print($r->requestID); ?>&unit=<?php print($selectedUnit); ?>&filter_status=<?php print($selectedFilter); ?>&sort=<?php print(array_key_exists('sort', $this->request) ? $this->request['sort'] : ''); ?>')" />
									</span>
								</div>
	<?php if ($r->getType() == "SCAN") { ?>
								<div class="denyButtons">
									<span>
										<input type="button" class="denyButton" value="Deny Copyright" onClick="confirmation('denyCopyright', 'index.php?cmd=setStatus&request_id=<?php print($r->requestID); ?>&unit=<?php print($selectedUnit); ?>&filter_status=<?php print($selectedFilter); ?>&sort=<?php print(array_key_exists('sort', $this->request) ? $this->request['sort'] : '');?>&status=DENIED')"/>
									</span>
									<span>
										<input type="button" class="denyButton"  value="Deny Copyright for all" onClick="confirmation('denyCopyrightAll', 'index.php?cmd=setStatus&request_id=<?php print($r->requestID); ?>&unit=<?php print($selectedUnit); ?>&filter_status=<?php print($selectedFilter); ?>&sort=<?php print(array_key_exists('sort', $this->request) ? $this->request['sort'] : ''); ?>&status=DENIED_ALL')"/>
									</span>
								</div>

	<?php } ?>
								<div class="searchButtons">
									<span class="searchTitleButton">
										<a href="./?cmd=doSearch&search[0][field]=title&search[0][test]=LIKE&search[0][term]=<?php print(htmlentities(trim($item->getTitle()))); ?>" target="_rdSearchTab">Title Search</a>
									</span>
									<?php if('' != trim($item->getLocalControlKey())) {?>
									<span class="searchControlButton">
										<a href="./?cmd=doSearch&search[0][field]=local_control_key&search[0][test]=EQ&search[0][term]=<?php print(htmlentities(trim($item->getLocalControlKey()))); ?>"  target="_rdSearchTab">Control# Search</a>
									</span>
									<?php } ?>
									<?php if('' != trim($item->getBarcode())) {?>
									<span class="searchControlButton">
										<a href="./?cmd=doSearch&search[0][field]=barcode&search[0][test]=EQ&search[0][term]=<?php print(htmlentities(trim($item->getBarcode()))); ?>"  target="_rdSearchTab">Barcode Search</a>
									</span>
									<?php } ?>
								</div>
							</div>
						</div>
<?php }?>

<?php if(!$this->printView){ ?>						
						<div class="queueMetadata">

							<div id='notice_<?php print($r->requestID); ?>' class='detailsButton'>
								<img class="detailsImg" width='16px' height='16px' src='public/images/maximize.gif' alt=''/>
								DETAILS
							</div>
							<div id='itemNotice_<?php print($item->getId()); ?>' class='editButton'>
								<a href="./?cmd=editItem&itemID=<?php  print($item->getId()); ?>"><img class="editImg" width='16px' height='16px' src='public/images/icons/pencil-small.png' alt=''/>
								EDIT ITEM</a>
							</div>
<?php } ?>
							<div class="queueMetadataBlock">
								<table>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">TITLE:</td>
										<td class="queueMetadataValue">
											<?php if ('' != trim($item->getControlNumber())) { ?><a target="_blank" href="<?php print($ils->getCatalogUrlFor($item->getControlNumber(), Rd_Ils::TITLE_CONTROL_KEY));?>"><?php } ?>
											<?php print($item->getTitle()); ?>
											<?php if ('' != trim($item->getControlNumber())) { ?></a><?php } ?>
										</td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">AUTHOR:</td>
										<td class="queueMetadataValue"><?php print($item->getAuthor()); ?></th>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Call Number:</td>
										<td class="queueMetadataValue"><?php print($callnum); ?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Volume Title:</td>
										<td class="queueMetadataValue"><?php print($item->getVolumeTitle());?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Volume Edition:</td>
										<td class="queueMetadataValue"><?php print($item->getVolumeEdition());?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Source/Year:</td>
										<td class="queueMetadataValue"><?php print($item->getSource()); ?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Pages/Times:</td>
										<td class="queueMetadataValue"><?php print($item->getPagesTimes()); ?></td>
									</tr>
<?php if(!$this->printView){?>
								</table>
								<table class="details">
<?php } else{?>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Status:</td>
										<td class="queueMetadataValue"><?php print($r->getStatus());?></td>
									</tr>
<?php }?>

									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">Loan Period:</td>
										<td class="queueMetadataValue"><?php print($r->reserve->getRequestedLoanPeriod());?></td>
									</tr>

									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">ISBN:</td>
										<td class="queueMetadataValue"><?php print($item->getISBN()); ?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">ISSN:</td>
										<td class="queueMetadataValue"><?php print($item->getISSN()); ?></td>
									</tr>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey">OCLC:</td>
										<td class="queueMetadataValue"><?php print($item->getOCLC()); ?></td>
									</tr>
<?php if($this->printView){?>
									<tr class= "queueMetadataRow">
										<td class="queueMetadataKey">Location:</td>
										<td class="queueMetadataValue"><?php print($location); ?></td>
									</tr>
<?php } else {?>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey" style="vertical-align: top">Barcodes:</td>
										<td class="queueMetadataValue"><?php print(implode(', ', $item->getAllBarcodes())); ?></td>
									</tr>
<?php } ?>
									<tr class="queueMetadataRow">
										<td class="queueMetadataKey" style="vertical-align: top">Notes:</td>
										<td class="queueMetadataValue"><?php print($this->getDisplayer()->renderNotes($requestNotes)); ?></td>
									</tr>
								</table>
							</div>
<?php if (!$this->printView){ //end queuemetadata block 
?>
						</div>
<?php }
	if(isset($selected)) {
		unset($$selected);
	}
?>
					</td>
				</tr>
<?php
		}
?>
</table>
</form>